<HTML>
<HEAD>
<TITLE>Reader Test</TITLE>
</HEAD>
<BODY id="bodyId">
 
<center>
<h1>Example Reader Used In Web </h1>
</center>
 
 
<br><br>
 
<center>

<p align="center"><textarea rows="20" id="TxtArea" cols="111"></textarea></p>

<script type="text/javascript" src="interface.js"></script>

<script> 
var msg = document.getElementById('TxtArea');
</script>

<script>
var obj = embed_reader.getOBJ(READER_TYPE._reader_type_compro);
obj.onResult(function(rData)
{
	switch(rData.FunctionID)
    {
		case FUNCIDS._fid_adaptReader:
			alert("Server is ready, you can start now!");
			break;
		case FUNCIDS._fid_init_ex:
		case FUNCIDS._fid_initialcom:
		{
			var opst;
			var rel = rData.RePara_Int;
			if(0 == rel)
			{
				var hdev = parseInt(rData.RePara_Str);
				if(hdev != -1)
				{
					icdev= hdev;
					obj.beep(icdev,10);   //do a beep
					msg.value =msg.value+"Link reader ok\n";
					isComOpen=true;             //Set reader link status
				}
				else
				{
					msg.value = "Link reader error\n";
					isComOpen=false;           //Set reader link failed status
				}
			}
			else
				msg.value = "Object load error\n";
			break;
		}
		case FUNCIDS._fid_exit:
			msg.value =msg.value+"reader closed\n"
			break;
		case FUNCIDS._fid_beep:
			break;
        case FUNCIDS._fid_getHWVer:
            msg.value =msg.value+"Version:" + rData.RePara_Str + "\n";
            break;
		case FUNCIDS._fid_srd_eeprom:
			msg.value =msg.value+"Read EEPROM:" + rData.RePara_Str + "\n";
			break;
		case FUNCIDS._fid_swr_eeprom:
			msg.value =msg.value+"Write EEPROM result:" + rData.RePara_Int + "\n";
			break;
        case FUNCIDS._fid_findCard_ex:
		case FUNCIDS._fid_findCardStr:
		{
			var strcard= rData.RePara_Str;
			if(strcard!="")
			{
				hasCard =true;
                msg.value=msg.value+"Card ID: "+strcard+"\n";
				//obj.halt(icdev);
				switch(gl_wantFunc)
				{
					case GFUNC.readClass:
					case GFUNC.writeClass:
					case GFUNC.initVal:
					case GFUNC.increment:
					case GFUNC.decrement:
					case GFUNC.readVal:
					case GFUNC.updateKey:
					obj.authenticationEx(icdev,0,gl_sector,"FFFFFFFFFFFF"); 
					break;
					case GFUNC.contectLessCpuCardTest:
					obj.fcpureset(icdev, 0);
					break;
				}
			}
			else
			{
				msg.value=msg.value+"Found no card \n";
				hasCard =false;        //Set no card status
			}
			break;
		}
		case FUNCIDS._fid_authenClassEx:
		{
			var rel = rData.RePara_Int;
			if(0 == rel)
			{
				switch(gl_wantFunc)
				{
					case GFUNC.readClass:
					obj.read(icdev,gl_BinBlock); 
					//obj.directRead(icdev,gl_BinBlock);        //Read data directly as binary
					break;
					case GFUNC.writeClass:
					obj.write(icdev,gl_BinBlock,"11111111111111111111111111111111");   
					//obj.directWrite(icdev,gl_BinBlock,"IC Card Reader  ");
					break;
					case GFUNC.initVal:
					obj.initialval(icdev,gl_valBlock,100);
					break;
					case GFUNC.increment:
					obj.increment(icdev,gl_valBlock,20);
					break;
					case GFUNC.decrement:
					obj.decrement(icdev,gl_valBlock,10);
					break;
					case GFUNC.readVal:
					obj.readval(icdev,gl_valBlock);
					break;
					case GFUNC.updateKey:
					obj.changkey(icdev,gl_sector,"FFFFFFFFFFFf","FF078069",0,"FFFFFFFFFFFF");
					break;
				}
			}
			else msg.value=msg.value+" Authen sector:" + gl_sector +" error \n";
			break;
		}
		
		case FUNCIDS._fid_readAsStr:
		{
			var data =  rData.RePara_Str;     
			if(data != "")
			{
				msg.value = msg.value + "Read ok, data: \n" + data + "\n";
			}
			else
			{
				msg.value = msg.value + "Read error\n";
			}
		}
		break;
		case FUNCIDS._fid_writeAsStr:
		{
			var iRet = rData.RePara_Int;   
			if(iRet == 0)
			{
				msg.value = msg.value + "Write card ok\n";
			}
			else
			{
				msg.value = msg.value + "Write card error\n";
			}
		}
		break;
		case FUNCIDS._fid_readAsHex:
		{
			var data =  rData.RePara_Str;     
			if(data != "")
			{
				msg.value = msg.value + "Read ok, data: \n" + data + "\n";
			}
			else
			{
				msg.value = msg.value + "Read error\n";
			}
		}
		break;
		case FUNCIDS._fid_writeAsHex:
		{
			var iRet = rData.RePara_Int;   
			if(iRet == 0)
			{
				msg.value = msg.value + "Write card ok\n";
			}
			else
			{
				msg.value = msg.value + "Write card error\n";
			}
		}
		break;
		case FUNCIDS._fid_initVal:
		{
			var iRet = rData.RePara_Int;   
			if(iRet == 0)
			{
				msg.value = msg.value + "Initial value ok\n";
			}
			else
			{
				msg.value = msg.value + "Initial value error\n";
			}
		}
		break;
		case FUNCIDS._fid_increment:
		{
			var iRet = rData.RePara_Int;   
			if(iRet == 0)
			{
				obj.transfer(icdev,gl_valBlock);
				msg.value = msg.value + "Increment value ok\n";
			}
			else
			{
				msg.value = msg.value + "Increment value error\n";
			}
		}
		break;
		case FUNCIDS._fid_decrement:
		{
			var iRet = rData.RePara_Int;   
			if(iRet == 0)
			{
				obj.transfer(icdev,gl_valBlock);
				msg.value = msg.value + "Decrement value ok\n";
			}
			else
			{
				msg.value = msg.value + "Decrement value error\n";
			}
		}
		break;
		case FUNCIDS._fid_readVal:
		{
			var iRet = rData.RePara_Int;   
			if(iRet > 0)
			{
				msg.value = msg.value + "Current value :" + iRet +"\n";
			}
			else
			{
				msg.value = msg.value + "Read value error\n";
			}
		}
		break;
		case FUNCIDS._fid_changeKey:
		{
			var iRet = rData.RePara_Int;   
			if(iRet == 0)
			{
				msg.value = msg.value + "Update key ok\n";
			}
			else
			{
				msg.value = msg.value + "Update key error\n";
			}
		}
		break;
		case FUNCIDS._fid_fcpuReset:
		{
			var iRet = rData.RePara_Str;   
			if(iRet != "")
			{
				msg.value = msg.value + "Cpu card reset:" + iRet + "\n";
				obj.fcpuCommandlink(icdev,5,"0084000008",7,60);
			}
			else
			{
				msg.value = msg.value + "Cpu card error\n";
			}
		}
		break;
		case FUNCIDS._fid_fcpuAPDU:
		{
			var iRet = rData.RePara_Str;   
			if(iRet != "")
			{
				msg.value = msg.value + "Apdu return:" + iRet + "\n";
			}
			else
			{
				msg.value = msg.value + "Apdu error\n";
			}
		}
		break;
		case FUNCIDS._fid_hexToStr:
		{
			var str = rData.RePara_Str;

			if(str != "")
			{
				msg.value = msg.value + "Hex to string ok, result: \n" + str + "\n";
			}
			else
			{
				msg.value = msg.value + "Hex to string error\n";
			}
		}
		break;
		case FUNCIDS._fid_strToHex:
		{
			var str = rData.RePara_Str;

			if(str != "")
			{
				msg.value = msg.value + "String to hex ok, result: \n" + str + "\n";
			}
			else
			{
				msg.value = msg.value + "String to hex error\n";
			}
		}
		break;
		case FUNCIDS._fid_crypt:
		{
			var str = rData.RePara_Str;

			if(str != "")
			{
				msg.value = msg.value + "Crypt ok, result: \n" + str + "\n";
			}
			else
			{
				msg.value = msg.value + "Crypt test error\n";
			}
		}
		break;
	}
	
}
);


//Link Reader
function Connect()
{
	try{
	if(isComOpen==false)          //if reader link failed
	{
	  obj.initialcom(2,115200);
		//obj.init_ex(2, "/dev/usb/hiddev0",115200);
	}
	}catch(e){alert(e.message);}

	return;
}
function GetVer()
{
    obj.getHWVer(icdev);
}
function ReadROM()
{
	obj.srd_eeprom(icdev, gl_romRWAddr, gl_romRWLen);
}
function WriteROM()
{
	var testStr = "This is a test string";
	var str_len = testStr.length;

	gl_romRWLen = str_len;

	obj.swr_eeprom(icdev, gl_romRWAddr, gl_romRWLen, testStr);
}

// Find card
function Findcard()
{
    //obj.findcardStr(icdev,1);//1);     //1: multy card mode
    obj.findCard_ex(icdev,1, 0x80);//1);     //1: multy card mode
	gl_wantFunc = 0;
}
//Read card
function Read()
{
	obj.findcardStr(icdev,1);//1);     //1: multy card mode
	gl_wantFunc = GFUNC.readClass;
}

//Write card
function Write()
{
	obj.findcardStr(icdev,1);//1);     //1: multy card mode
	gl_wantFunc = GFUNC.writeClass;
}

//Disconnect with reader
function Disconnect()
{
	iRet = obj.exit(icdev);
    isComOpen=false; //Set unlink status
}

function InitVal()
{
	obj.findcardStr(icdev,1);//1);     //1: multy card mode
	gl_wantFunc = GFUNC.initVal;
}

function Increment()
{
	obj.findcardStr(icdev,1);//1);     //1: multy card mode
	gl_wantFunc = GFUNC.increment;
}

function Decrement()
{
	obj.findcardStr(icdev,1);//1);     //1: multy card mode
	gl_wantFunc = GFUNC.decrement;
}

function ReadVal()
{
	obj.findcardStr(icdev,1);//1);     //1: multy card mode
	gl_wantFunc = GFUNC.readVal;
}

function Changekey()
{
	obj.findcardStr(icdev,1);//1);     //1: multy card mode
	gl_wantFunc = GFUNC.updateKey;
}

function ContanctLessCPUCardTest()
{
	obj.findcardStr(icdev,1);//1);     //1: multy card mode
	gl_wantFunc = GFUNC.contectLessCpuCardTest;
}

function Hex2Str()
{
  var bytes = "test1";
  
  obj.charToHex(bytes, 5*2);
}

function Str2Hex()
{
 var str = "7465737431";
  
  obj.hexTochar(str, 5);
}

function cryptTest()
{
 var strSrc = "0102030405060708090a0b0c0d0e0f10";
 var strIv = "0102030405060708";
 var fEn_De = 1;//0-decrypt ,1-encrypt
 var alg = 3;//0-DES_ECB, 1-DES_CBC, 2-3DES_ECB, 3-3DES_CBC,4-AES_ECB,5-AES_CBC
  
  obj.crypt(icdev, fEn_De, alg, strSrc, strIv);
}

var GFUNC = {
	readClass:1,
	writeClass:2,
	initVal:3,
	increment:4,
	decrement:5,
	readVal:6,
	updateKey:7,
	contectLessCpuCardTest:8,
};

 var nRead =0;     //The count one card repeat find
 var hasCard =false; //A flag indicate whether the reader find card
 var isComOpen=false; //A flag indicate whether the reader is open or not
 var icdev= -1; //The handle value which band with reader
 var gl_sector = 2;//The sector number of card for testing
 var gl_BinBlock = 8;//The binary block number for read/write testing
 var gl_valBlock = 9; //The value block number for read/write testing
 var gl_romRWLen = 10; //The length of memory in reader for Reading/Writting
 var gl_romRWAddr = 0; //The address of memory in reader for Reading/Writting
 var gl_wantFunc =  0; //A flag to indentify what for do


</script>
 
<br>
<form name="formname">

<p align="center">
<input type="button" value="  Link  " name="btnConnect" onclick="Connect()">&nbsp;  
<input type="button" value=" GetVer " name="btn_GetVer" onclick="GetVer()">&nbsp;
<input type="button" value=" FindCard " name="btn_FindCard" onclick="Findcard()">&nbsp;
<input type="button" value="  ReadCard  " name="btn_Read" onclick="Read()">&nbsp; 
<input type="button" value=" WriteCard " name="btn_Write" onclick="Write()">&nbsp;
<input type="button" value=" InitVal " name="btn_InitVal" onclick="InitVal()">&nbsp;
<input type="button" value=" Increment " name="btn_Increment" onclick="Increment()">&nbsp;
<input type="button" value=" Decrement " name="btn_Decrement" onclick="Decrement()">&nbsp;
<input type="button" value=" ReadVal " name="btn_ReadVal" onclick="ReadVal()">&nbsp;
<input type="button" value=" UpdateKey "name="btn_changekey" onclick="Changekey()">&nbsp;
<input type="button" value=" UnLink " name="btn_Disconnect" onclick="Disconnect()">
</p> 

<p align="center">
<input type="button" value=" Contactless CPU Card Test " name="btnContactLessCPUCardTest" onclick="ContanctLessCPUCardTest()">
</p>

<p align="center">
<input type="button" value="Write ROM" name="btnWriteROM" onclick="WriteROM()">&nbsp;
<input type="button" value="Read ROM" name="btnReadROM" onclick="ReadROM()">&nbsp;
</p>

<p align="center">
<input type="button" value="hex2Str" name="btnHex2Str" onclick="Hex2Str()">&nbsp;
<input type="button" value="str2Hex" name="btnStr2Hex" onclick="Str2Hex()">&nbsp;
<input type="button" value="crypt" name="btnCryptTest" onclick="cryptTest()">&nbsp;
</p>
</form>
 
</center>
 
</BODY>
</HTML>

